package com.lhl.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lhl.dao.StudentDao;
import com.lhl.dao.StudentDaoImpl;
import com.lhl.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet(urlPatterns = "/allStudent")
public class AllStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        StudentDao dao = new StudentDaoImpl();
        List<Student> students = dao.queryAllStudents();
        response.getWriter().print(
                new ObjectMapper().setDateFormat(
                        new SimpleDateFormat("yyyy-MM-dd")
                ).writeValueAsString(students)
        );
    }
}
